package com.idbk.chargestation.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.idbk.chargestation.activity.pile.ActivityPointMsg;
import com.idbk.chargestation.amap.util.AMapResultCode;
import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.R;
import com.idbk.chargestation.amap.base.BaseMapGeographyActivity;
import com.idbk.chargestation.util.Const;

import java.util.Locale;

/**
 * 充电点采集
 * @author lupc, zhx
 *
 */
public class ActivityPointCollection extends BaseMapGeographyActivity {

	public static final String CHARGE_COLLECTION_ADDRESS = "charge_collection_address";
	public static final String CHARGE_COLLECTION_PROVINCE = "charge_collection_province";
	public static final String CHARGE_COLLECTION_CITY = "charge_collection_city";
	public static final String CHARGE_COLLECTION_COUNTRY = "charge_collection_country";
	public static final String CHARGE_COLLECTION_LNG = "charge_collection_lng";
	public static final String CHARGE_COLLECTION_LAT = "charge_collection_lat";
	public final static String FROM_SEARCH_LIST_LAT = "from_search_list_lat";
	public final static String FROM_SEARCH_LIST_LON = "from_search_list_lon";
	
	private TextView mTextLat, mTextLng, tvAddress;//经纬度显示

	private String mProvince, city, country, address;
	private double longitude, latitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_point_collection);

		setupView(savedInstanceState);

		setupData();
	}

	@SuppressWarnings("ConstantConditions")
	private void setupView(Bundle savedInstanceState) {
		setupToolBar2();//标题栏设置
		initMapView(savedInstanceState);
		setCameraChangeListener();
		initLocationStyle();
		initGeocodeSearch();
		
		findViewById(R.id.textview_ok).setOnClickListener(this);
		findViewById(R.id.imagebutton_location).setOnClickListener(this);
		
		mTextLng = (TextView) findViewById(R.id.charge_collection_lng);
		mTextLat = (TextView) findViewById(R.id.charge_collection_lat);
		tvAddress = (TextView) findViewById(R.id.charge_collection_address);		
	}

	private void setupData() {
	
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == Const.CODE_REQUEST_LOCATE && 
				resultCode == Const.CODE_REQUEST_LOCATE_SUCCESS) {
			Bundle bundle = data.getExtras();
			double lng = bundle.getDouble(FROM_SEARCH_LIST_LON);
			double lat = bundle.getDouble(FROM_SEARCH_LIST_LAT);
			aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 16));
		}
	}


	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.imagebutton_location://重新定位
			AppContext ac = AppContext.getInstance();
			double lat = ac.getLat();
			double lng = ac.getLng();
			aMap.animateCamera(CameraUpdateFactory.changeLatLng(new LatLng(lat, lng)));
			break;
		case R.id.textview_ok:
			if(!tvAddress.getText().toString().equals("获取当前地址中…") && 
					!tvAddress.getText().toString().equals("")) {
				putLocationMsg();
			}
			break;
		default:
			break;
		}
	} 

	//传入位置相关信息到下一Activity
	private void putLocationMsg() {
		Intent intent = new Intent(this, ActivityPointMsg.class);
		intent.putExtra(ActivityPointMsg.KEY_MODEL, ActivityPointMsg.MODEL_ADD);
		intent.putExtra(CHARGE_COLLECTION_ADDRESS, address);
		
		intent.putExtra(CHARGE_COLLECTION_PROVINCE, mProvince);
		intent.putExtra(CHARGE_COLLECTION_CITY, city);
		intent.putExtra(CHARGE_COLLECTION_COUNTRY, country);
		
		intent.putExtra(CHARGE_COLLECTION_LNG, longitude);
		intent.putExtra(CHARGE_COLLECTION_LAT, latitude);
		startActivity(intent);
	}

	@Override
	public void onCameraChange(CameraPosition position) {
		tvAddress.setText("获取当前地址中…");
		LatLng latLng = position.target;//获取屏幕中心点
		longitude = latLng.longitude;
		latitude = latLng.latitude;
		mTextLng.setText(String.format(Locale.CHINA, "经度：%.6f", longitude));
		mTextLat.setText(String.format(Locale.CHINA, "纬度：%.6f", latitude));
	}

	@Override
	public void onCameraChangeFinish(CameraPosition position) {
		LatLng latLng = position.target;//获取屏幕中心点
		RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(latLng.latitude, latLng.longitude), 
				200, GeocodeSearch.AMAP);
		mGeocodeSearch.getFromLocationAsyn(query);
	}

	@Override
	public void onGeocodeSearched(GeocodeResult result, int rCode) {
	}

	@Override
	public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
		System.out.print(rCode);
		if(rCode == AMapResultCode.OK) {  //正常返回
			if(result != null && result.getRegeocodeAddress() != null && 
					result.getRegeocodeAddress().getFormatAddress() != null) {
				String addressName = result.getRegeocodeAddress().getFormatAddress();
				tvAddress.setText(addressName);
				address = addressName;
				mProvince = result.getRegeocodeAddress().getProvince();
				city = result.getRegeocodeAddress().getCity();
				country = result.getRegeocodeAddress().getDistrict();
			}
		}
	}

}
